﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KalenderTradisional
{
    class Jawa
    {
        private int hari;
        private int tanggal;
        private int bulan;
        private int tahun;
        private string hariJAwa;
        private string tanggalJawa;
        private string bulanJawa;
        private string tahunJawa;
        DateTime calendar;

        public Jawa(int tahun, int bulan, int tanggal)
        {
            calendar = new DateTime(tahun, bulan, tanggal);
            this.hari = (int)calendar.DayOfWeek;
            this.tanggal = tanggal;
            this.bulan = bulan;
            this.tahun = tahun;
            setKalender(this.tahun, this.bulan, this.tanggal, this.hari);
        }

        public string getHari()
        {
            return this.hariJAwa;
        }

        public string getTanggal()
        {
            return this.tanggalJawa;
        }

        public string getBulan()
        {
            return this.bulanJawa;
        }

        public string getTahun()
        {
            return this.tahunJawa;
        }

        private int getHrBulan(int val, int val2)
        {
            int result = 0;

            for (int i = 1; i <= val; i++)
            {
                if (i == 2 && val2 == 1) //kabisat
                {
                    result += 29;
                }
                else if (i == 2 && val2 == 0) //basitoh
                {
                    result += 28;
                }
                else if (i < 8 && i % 2 == 1) //sebelum Agustus
                {
                    result += 31;
                }
                else if (i >= 8 && i % 2 == 0) // Agustus dan sesudahnya
                {
                    result += 31;
                }
                else
                {
                    result += 30;
                }
            }
            return result;
        }

        private int getKabisat(int val)
        {
            int result = 0;
            for (int i = 2; i <= val; i++)
            {
                if (i == 2 || i == 5 || i == 7 || i == 10 || i == 13 || i == 16
                    || i == 18 || i == 21 || i == 24 || i == 26 || i == 29)
                    result++;
            }
            return result;
        }

        public void setKalender(int tahun, int bulan, int tanggal, int hari)
        {
            int tgl, bln, thn, siklus;
            double sikThn = 0;
            int hrSiklus, hrTahun, hrBulan, hrTot;
            int hslHari, hslPasaran;

            tgl = tanggal;
            bln = bulan - 1;
            thn = tahun - 1;

            siklus = thn / 4;
            sikThn = (((double)thn / 4.0) % 1) * 4;

            //hitung total hari
            hrSiklus = siklus * 1461;
            hrTahun = 365 * (int)sikThn;

            //kabisat
            if (tahun % 4 == 0)
            {
                hrBulan = getHrBulan(bln, 1);
            }
            //basitoh
            else
            {
                hrBulan = getHrBulan(bln, 0);
            }

            hrTot = tgl + hrBulan + hrTahun + hrSiklus;

            int hrTotHij, thnHij, blnHij = 1, tglHij;
            int tafawut = 227016;
            int gregorian = 13;
            int daur = 10631;

            hrTotHij = hrTot - tafawut - gregorian;
            thnHij = (hrTotHij / daur) * 30;
            thnHij += ((hrTotHij % daur) / 354) + 1;

            int val = (hrTotHij % daur) / 354;
            int sisaHari;
            sisaHari = (hrTotHij % daur) % 354 - getKabisat(val);

            for (int i = 0; i < 12; i++)
            {
                if (i % 2 == 1 && sisaHari > 29)
                {
                    sisaHari -= 29;
                    blnHij++;
                }
                else if (i % 2 == 0 && sisaHari > 30)
                {
                    sisaHari -= 30;
                    blnHij++;
                }
                else
                {
                    setTanggal(sisaHari);
                    setBulan(blnHij); 
                    //setTglJawa(sisaHari);
                    //setBulanJawa(getBulanJawa(blnHij));
                    break;
                }

            }

            //setTanggal(sisaHari); // @param : sisa hari
            setHari(hrTot); // @param : hari total
            //setBulan(blnHij); // @param : blnHij
            setTahun(thnHij); // @param : thnHij
        }

        public void setHari(int val)
        {
            int hrTot = val;
            int hslHari = hrTot % 7;
            int hslPasaran = hrTot % 5;

            switch (hslPasaran)
            {
                case 0: this.hariJAwa = "LEGI"; break;
                case 1: this.hariJAwa = "PAHING"; break;
                case 2: this.hariJAwa = "PON"; break;
                case 3: this.hariJAwa = "WAGE"; break;
                case 4: this.hariJAwa = "KLIWON"; break;
                default: this.hariJAwa = ""; break;
            }

            if (hslHari == 0) hslHari = 7;

            switch ((hslHari - 1) % 7)
            {
                case 0: this.hariJAwa = "MINGGU" + " " + this.hariJAwa; break;
                case 1: this.hariJAwa = "SENIN" + " " + this.hariJAwa; break;
                case 2: this.hariJAwa = "SELASA" + " " + this.hariJAwa; break;
                case 3: this.hariJAwa = "RABU" + " " + this.hariJAwa; break;
                case 4: this.hariJAwa = "KAMIS" + " " + this.hariJAwa; break;
                case 5: this.hariJAwa = "JUMAT" + " " + this.hariJAwa; break;
                case 6: this.hariJAwa = "SABTU" + " " + this.hariJAwa; break;
                default: this.hariJAwa = ""; break;
            }
        }

        public void setTanggal(int val)
        {
            this.tanggalJawa = val.ToString();
        }

        public void setBulan(int val) {
            switch (val)
            {
                case 1: this.bulanJawa = "MUHARAM"; break;
                case 2: this.bulanJawa = "SAFAR"; break;
                case 3: this.bulanJawa = "RABIUL AWAL"; break;
                case 4: this.bulanJawa = "RABIUL AKHIR"; break;
                case 5: this.bulanJawa = "JUMADIL AWAL"; break;
                case 6: this.bulanJawa = "JUMADIL AKHIR"; break;
                case 7: this.bulanJawa = "RAJAB"; break;
                case 8: this.bulanJawa = "SYA'BAN"; break;
                case 9: this.bulanJawa = "RAMADHAN"; break;
                case 10: this.bulanJawa = "SYAWAL"; break;
                case 11: this.bulanJawa = "DZULKAIDAH"; break;
                case 12: this.bulanJawa = "DZULHIJJAH"; break;
                default: this.bulanJawa = ""; break;
            }
        }

        public void setTahun(int val)
        {
            this.tahunJawa = val.ToString() + "H";
        }
      

        
    }
}
